package com.example.juyixia;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	private List<Map<String, Object>> data;
    private LayoutInflater layoutInflater;
    private Context context;
   // private Myinfo myinfo=(Myinfo) context.getApplicationContext();
    public MyAdapter(Context context, List<Map<String, Object>> data) {
    //传入的data，就是我们要在listview中显示的信息
        this.context = context;
        this.data = data;
        this.layoutInflater = LayoutInflater.from(context); 
    }
    //这里定义了一个类，用来表示一个item里面包含的东西，像我的就是一个imageView和三个TextView，按自己需要来
    public class Info {
        public ImageView image;
        public TextView username;
        public TextView userid;
    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		 return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		 return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		 Info info = new Info();
	        convertView = layoutInflater.inflate(R.layout.item_friends, null);
	        info.image = (ImageView) convertView.findViewById(R.id.headpic);
	        info.userid = (TextView) convertView
	                .findViewById(R.id.usersfriendid);
	        info.username = (TextView) convertView
	                .findViewById(R.id.usersfriendname);
         
	        //设置数据
	        info.image.setImageResource((Integer) data.get(position).get("image"));
	        info.userid.setText((String) data.get(position).get(
	                "userid"));
	        info.username.setText((String) data.get(position).get(
	                "username"));
	        
	        info.image.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent=new Intent(context, Activity_chatActivity.class);
					 Bundle bundle=new Bundle();
		             bundle.putString("name", data.get(position).get("username").toString());
		             bundle.putString("ip", data.get(position).get("userip").toString());
		             intent.putExtras(bundle);
	                //利用上下文开启跳转
	                context.startActivity(intent);
				}
			});
	        return convertView;
	}

}
